﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Jw.Models;
using Jw.Web.Areas.Senv.Common;
using System.Web.Http;
using Jw.Core;
using Newtonsoft.Json.Linq;
using Jw.Web.App_Start;

namespace Jw.Web.Areas.Senv.Controllers
{
    public class ZybdjtzController : BaseController
    {
        //
        // GET: /Senv/Zybdjtz/

        public ActionResult Index()
        {
            var codeService = new sys_codeService();
            var model = new
            {
                idField = "Zybdjtz_BillNo", //主键
                urls = SenvHelper.GetIndexUrls("Zybdjtz"),
                resx = SenvHelper.GetIndexResx("职业病登记台帐"),
                dataSource = new
                {
                    yearItems = codeService.GetYearItems(),
                    monthItems = codeService.GetMonthItems()
                },
                form = new
                {
                    Zybdjtz_BillNo = "", //单据编号
                    Zybdjtz_Jcrq = "",   //登记日期
                    Zybdjtz_Qzrq = "",   //确诊日期
                    Zybdjtz_Zyblb="", //职业病类型
                    Zybdjtz_Zybjb="",   //职业病级别
                    Zybdjtz_Zdzmbh = ""  //备注说明
                }

            };
            return View(model);
        }
         [MvcMenuFilter(false)]
        public ActionResult Edit(string id = "")
        {
            var data = new ZybdjtzApiController().GetEditMaster(id);
            var codeService = new sys_codeService();

            var model = new
            {
                form = data.form,
                scrollKeys = data.scrollKeys,
                urls = SenvHelper.GetEditUrls("Zybdjtz"),
                resx = SenvHelper.GetEditResx("职业病登记台帐"),
                dataSource = new
                {
                    //measureUnit = codeService.GetMeasureUnitListByType(),
                    //materialUse = codeService.GetValueTextListByType("MaterialUse"),
                    //billingUnit = codeService.GetValueTextListByType("BillingUnit")
                },
                defaultForm = new s_Zybdjtz().Extend(new
                {
                    Zybdjtz_BillNo = id, //简报编号
                    //Aqjb_Jbnr = "", //简报内容,
                    Zybdjtz_Jcrq = DateTime.Now.Date.ToString("yyyy-MM-dd"), //检查日期
                    Zybdjtz_Qzrq = DateTime.Now.ToString("yyyy-MM-dd") //确诊日期
                }),
                defaultRow = new
                {
                },
                setting = new
                {
                    postFormKeys = new string[] { "Zybdjtz_BillNo" },
                }
            };
            return View(model);
        }

    }

    public class ZybdjtzApiController : ApiController
    {
        public string GetNewBillNo()
        {
            return new s_ZybdjtzService().GetNewKey("Zybdjtz_BillNo", "dateplus");
        }

        public dynamic Get(RequestWrapper query)
        {
            query.LoadSettingXmlString(@"
            <settings defaultOrderBy='Zybdjtz_BillNo DESC'>
            <select>*</select>
                <from>s_Zybdjtz</from>
                    <where defaultForAll='true' defaultCp='equal' defaultIgnoreEmpty='true' >
                        <field name='Zybdjtz_BillNo'     cp='like'></field>
                        <field name='Zybdjtz_Jcrq' cp='daterange'></field>
                    </where>
            </settings>");
            var service = new s_ZybdjtzService();
            var pQuery = query.ToParamQuery();
            var result = service.GetDynamicListWithPaging(pQuery);

            return result;
        }

        public dynamic GetEditMaster(string id)
        {
            s_Zybdjtz obj = new s_ZybdjtzService().GetModel(ParamQuery.Instance().AndWhere("Zybdjtz_BillNo", id));
            return new
            {
                form = obj,
                scrollKeys = new s_ZybdjtzService().ScrollKeys("Zybdjtz_BillNo", id.ToString())
            };
        }
        public void Edit(dynamic data)
        {
            var formWrapper = RequestWrapper.Instance().LoadSettingXmlString(@"
            <settings>
                <table>{0}</table>
                <where><field name='Zybdjtz_BillNo' cp='equal'></field></where>
            </settings>", typeof(s_Zybdjtz).Name);
            var result = new s_ZybdjtzService().Edit(formWrapper, null, data);
        }
        /// <summary>
        /// 删除主表（删除主表前是删除明细)
        /// </summary>
        /// <param name="id"></param>
        public void Delete(string id)
        {
            var result = new s_ZybdjtzService().Delete(ParamDelete.Instance().AndWhere("Zybdjtz_BillNo", id));
            SenvHelper.ThrowHttpExceptionWhen(result <= 0, "单据删除失败[Aqjb_BillNo={0}]，请重试或联系管理员！", id);
        }
        //审核
        [System.Web.Http.HttpPost]
        public void Audit(string id, JObject data)
        {
            var status = data["status"].ToString();
            var comment = data["comment"].ToString();
            var result = new SenvService().Audit(typeof(s_Zybdjtz).Name, "Zybdjtz_BillNo", id, status, comment);
            SenvHelper.ThrowHttpExceptionWhen(result < 0, "单据审核失败[Zybdjtz_BillNo={0}]，请重试或联系管理员！", id);
        }
    }
}
